package com.fivefu.core.data.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.core.data.entity.DbFfskyeyeDevice;
import com.fivefu.core.data.entity.DbFfskyeyeRegion;
import com.fivefu.core.data.entity.DbFfskyeyeRegionDevice;
import com.fivefu.core.data.mapper.DbFfskyeyeRegionDeviceMapper;
import com.fivefu.core.data.services.DbFfskyeyeRegionDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fivefu.core.data.services.DbFfskyeyeRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 域设备关联表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-05 13:39:24
 */
@Service
public class DbFfskyeyeRegionDeviceServiceImpl extends ServiceImpl<DbFfskyeyeRegionDeviceMapper, DbFfskyeyeRegionDevice> implements DbFfskyeyeRegionDeviceService {

    @Autowired
    private DbFfskyeyeRegionService dbFfskyeyeRegionService;

    /**
     * 删除设备与域关联关系
     * @param deviceList2
     */
    @Override
    public void deleteCollectRegionDevice(List<DbFfskyeyeDevice> deviceList2) {
        if (null == deviceList2 || deviceList2.size() <= 0) {
            return;
        }
        deviceList2.forEach(k -> {
            deleteRegionDevice(k.getId());
        });
    }

    /**
     * 新增域设备关联关系
     * @param formatList
     */
    @Override
    public void insertCollectRegionDevice(List<DbFfskyeyeDevice> formatList,String platFormCode) {
        if (null == formatList || formatList.size() <= 0) {
            return;
        }
        List<DbFfskyeyeRegionDevice> regionDeviceList = new ArrayList<>();
        LocalDateTime time = LocalDateTime.now();
        formatList.forEach(k -> {
            DbFfskyeyeRegionDevice regionDevice = new DbFfskyeyeRegionDevice();
            regionDevice.setCreatedBy("sys");
            regionDevice.setCreatedTime(time);
            regionDevice.setUpdatedBy("sys");
            regionDevice.setUpdatedTime(time);
            regionDevice.setIsDelete(0);
            regionDevice.setDeviceId(k.getId());
            DbFfskyeyeRegion dbFfskyeyeRegion = dbFfskyeyeRegionService.queryRegion(k.getPlatformRegionCode(),platFormCode);
            regionDevice.setRegionId(dbFfskyeyeRegion.getId());
//            regionDevice.setRegionId(id);
            regionDeviceList.add(regionDevice);
        });
        saveBatch(regionDeviceList);
    }

    /**
     * 删除其他域下关联的设备信息
     * @param regionList
     */
    @Override
    public void deleteRedundantRegionDevice(List<DbFfskyeyeRegion> regionList,String userId) {
        if (null == regionList || regionList.size() <= 0) {
            return;
        }
        List<Long> regionIds = new ArrayList<>();
        regionList.forEach(k -> {
            regionIds.add(k.getId());
        });
        QueryWrapper<DbFfskyeyeRegionDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (!regionIds.isEmpty()) {
            wrapper.notIn("region_id",regionIds);
        }

        DbFfskyeyeRegionDevice regionDevice = new DbFfskyeyeRegionDevice();
        LocalDateTime time = LocalDateTime.now();
        regionDevice.setUpdatedBy(userId);
        regionDevice.setUpdatedTime(time);
        regionDevice.setIsDelete(1);
        baseMapper.update(regionDevice,wrapper);
    }

    /**
     * 根据设备id删除相关数据
     * @param deviceId
     */
    private void deleteRegionDevice(Long deviceId) {
        DbFfskyeyeRegionDevice regionDevice = new DbFfskyeyeRegionDevice();
        LocalDateTime time = LocalDateTime.now();
        regionDevice.setIsDelete(1);
        regionDevice.setUpdatedBy("sys");
        regionDevice.setUpdatedTime(time);
        QueryWrapper<DbFfskyeyeRegionDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("device_id",deviceId);
        baseMapper.update(regionDevice,wrapper);
    }

    /**
     * 编辑域设备关联关系
     * @param deviceId  设备id
     * @param regionId  域id
     */
    @Override
    public void editorRegionDevice(Long deviceId, Long regionId, String userid) {
        LocalDateTime time = LocalDateTime.now();
        DbFfskyeyeRegionDevice regionDevice = new DbFfskyeyeRegionDevice();
        DbFfskyeyeRegionDevice oldRegionDevice = queryRegionDevice(deviceId,regionId);
        regionDevice.setUpdatedTime(time);
        regionDevice.setUpdatedBy(userid);
        regionDevice.setDeviceId(deviceId);
        regionDevice.setRegionId(regionId);

        if (null != oldRegionDevice) {
            regionDevice.setId(oldRegionDevice.getId());
            baseMapper.updateById(regionDevice);
        } else {
            regionDevice.setCreatedTime(time);
            regionDevice.setCreatedBy(userid);
            regionDevice.setIsDelete(0);
            baseMapper.insert(regionDevice);
        }
    }

    /**
     * 删除多余的域设备关联关系
     * @param idList
     */
    @Override
    public void deleteRegionDevice(List<Long> idList) {
        QueryWrapper<DbFfskyeyeRegionDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (!idList.isEmpty()) {
            wrapper.notIn("device_id",idList);
        }

        DbFfskyeyeRegionDevice regionDevice = new DbFfskyeyeRegionDevice();
        regionDevice.setIsDelete(1);
        regionDevice.setUpdatedBy("sys");
        regionDevice.setUpdatedTime(LocalDateTime.now());
        baseMapper.update(regionDevice,wrapper);
    }

    /**
     * 根据设备id查询域设备信息
     * @param deviceId  设备id
     * @param regionId 域编号
     * @return
     */
    private DbFfskyeyeRegionDevice queryRegionDevice(Long deviceId, Long regionId) {
        QueryWrapper<DbFfskyeyeRegionDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("device_id",deviceId);
        wrapper.eq("region_id",regionId);
        DbFfskyeyeRegionDevice regionDevice = baseMapper.selectOne(wrapper);
        return regionDevice;
    }

    /**
     * 通过域编号查询设备信息
     * @param regionId 域编号
     * @return
     */
    @Override
    public List<DbFfskyeyeRegionDevice> queryRegionDeviceList(Long regionId){
        QueryWrapper<DbFfskyeyeRegionDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (regionId != null) {
            wrapper.eq("region_id",regionId);
        }
        List<DbFfskyeyeRegionDevice> list = baseMapper.selectList(wrapper);
        return list;
    }

    /**
     * 通过设备编号删除关联关系
     * @param deviceId 设备编号
     * @param userid 操作用户编号
     */
    @Override
    public void deleteRegionDeviceByDeviceId(Long deviceId, String userid) {
        DbFfskyeyeRegionDevice regionDevice = new DbFfskyeyeRegionDevice();
        regionDevice.setIsDelete(1);
        regionDevice.setUpdatedBy(userid);
        regionDevice.setUpdatedTime(LocalDateTime.now());

        QueryWrapper<DbFfskyeyeRegionDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("device_id",deviceId);
        baseMapper.update(regionDevice,wrapper);
    }
}
